﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using TTBusiness.DB_Linq;
namespace TTBusiness.ClassDB
{
    /// <summary>
    /// Create by ThiepND 23/3/2011
    /// </summary>
    public class clsBSNews
    {
        TTMarketing_DBDataContext objDB = new TTMarketing_DBDataContext();
        public List<tblNew> getAll()
        {
            try
            {
                var list = from a in objDB.tblNews
                           select a;
                return list.ToList<tblNew>();
            }
            catch (Exception)
            {

                throw;
            }
        }
        
        public List<tblNew> getByStatus(bool status)
        {
            try
            {
                var list = from a in objDB.tblNews
                           where a.Status.Equals(status)
                           orderby a.ID descending
                           select a;
                return list.ToList<tblNew>();
            }
            catch (Exception)
            {

                throw;
            }
        }
        public List<tblNew> getByCate(bool status, int cateId)
        {
            try
            {
                var list = from a in objDB.tblNews
                           where a.Status.Equals(status) && a.CateID.Equals(cateId)
                           orderby a.ID descending
                           select a;
                return list.ToList<tblNew>();
            }
            catch (Exception)
            {

                throw;
            }
        }

        public tblNew getById(int id)
        {
            try
            {
                var obj = objDB.tblNews.SingleOrDefault(a => a.ID == id);
                return obj;

            }
            catch (Exception)
            {

                throw;
            }
        }
        public tblNew getByTitle(String title)
        {
            try
            {
                var obj = objDB.tblNews.SingleOrDefault(a => a.Title == title);
                return obj;

            }
            catch (Exception)
            {

                throw;
            }
        }

        public void insert(tblNew obj)
        {
            try
            {
                obj.Date = DateTime.Now;
                objDB.tblNews.InsertOnSubmit(obj);
                objDB.SubmitChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }

        public void update(tblNew obj)
        {
            try
            {
                var objj = objDB.tblNews.SingleOrDefault(a => a.ID == obj.ID);
                if (objj != null)
                {
                    objj.Detail = obj.Detail;
                    objj.ID = obj.ID;
                    objj.Image = obj.Image;
                    objj.Title = obj.Title;
                    objj.Summary = obj.Summary;
                    objj.Status = obj.Status;
                    objj.CateID = obj.CateID;
                    objj.Hot = obj.Hot;
                    objj.Date = DateTime.Now;
                    objDB.SubmitChanges();
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        public void update(int id, bool status)
        {
            try
            {
                var obj = objDB.tblNews.SingleOrDefault(a => a.ID == id);
                if (obj != null)
                {
                    obj.Status = status;
                    objDB.SubmitChanges();
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        public void delete(int id)
        {
            try
            {
                var obj = (from a in objDB.tblNews
                           where a.ID == id
                           select a).SingleOrDefault();
                objDB.tblNews.DeleteOnSubmit(obj);
                objDB.SubmitChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
